﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using WSManager.Model;
using WSManager.DataAccess;

namespace WSManager.Wpf
{
    public class ViewModel : ViewModelBase<object>
    {
        WebRepository webRepo = new FilesystemWebRepository();
        public ViewModel()
        {
            if (!string.IsNullOrEmpty(Properties.Settings.Default.SelectedWebName))
            {
                var w = webRepo.GetWeb(Properties.Settings.Default.SelectedWebName);
                if (w != null)
                    SelectedWeb = new WebViewModel(w);
            }
        }

        #region "Properties"
        public IEnumerable<Web> Webs
        {
            get
            {
                return webRepo.GetWebs();
            }
        }

        private Web _selectedWeb;
        public WebViewModel SelectedWeb
        {
            get
            {
                return _selectedWeb == null ? null : new WebViewModel(_selectedWeb);
            }
            set
            {
                if (value == null) throw new ArgumentNullException();
                if (_selectedWeb != null && AutoSave)
                {
                    var s = _selectedWeb;
                    ThreadPool.QueueUserWorkItem(g => webRepo.SaveWeb(s));
                }
                _selectedWeb = value.Web;
                RaisePropertyChanged("SelectedWeb");
                Properties.Settings.Default.SelectedWebName = _selectedWeb.Name;
            }
        }
        public string SelectedWebName
        {
            get
            {
                return _selectedWeb == null ? null : _selectedWeb.Name;
            }
            set
            {
                if (_selectedWeb != null && AutoSave)
                {
                    var s = _selectedWeb;
                    webRepo.SaveWeb(s);
                }
                _selectedWeb = webRepo.GetWeb(value);
                RaisePropertyChanged("SelectedWeb", "SelectedWebName");
                if (value != null) Properties.Settings.Default.SelectedWebName = _selectedWeb.Name;
                else Properties.Settings.Default.SelectedWebName = null;
            }
        }
        public bool AutoSave
        {
            get { return Properties.Settings.Default.AutoSave; }
            set
            {
                Properties.Settings.Default.AutoSave = value;
                RaisePropertyChanged("AutoSave");
            }
        }

        #endregion

        #region "Methods"

        public ICommand NewWebCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var dialog = new Dialogs.CreateWeb(w => {
                        webRepo.AddWeb(w);
                        RaisePropertyChanged("Webs");
                        SelectedWebName = w.Name;
                    });
                    dialog.Show();
                });
            }
        }

        public ICommand RemoveWebCommand
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        if (MessageBox.Show("Are you sure?", "Web removing", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            webRepo.Remove(_selectedWeb.Name);
                            RaisePropertyChanged("Webs");
                        }
                    },
                    () => true);
            }
        }

        public ICommand SaveCommand { get { return new RelayCommand(SaveSelectedWeb, () => this._selectedWeb != null); } }

        public ICommand DiscardChangesCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    _selectedWeb = webRepo.LoadWeb(_selectedWeb.Name);
                    RaisePropertyChanged("SelectedWeb");
                },
                () => _selectedWeb != null);
            }
        }

        public void SaveSelectedWeb()
        {
            webRepo.SaveWeb(_selectedWeb);
        }

        public ICommand EditMetaDefinitions
        {
            get
            {
                return new RelayCommand(() =>
                {
                    new Dialogs.EditDefinitions(_selectedWeb.Metadata.ArticleMetadataDefinition).Show();
                }, () => _selectedWeb != null);
            }
        }

        #endregion
    }
}
